from flask import Blueprint, request, jsonify, redirect
import json
from dbs import dbs

bp_login = Blueprint('login', __name__)


@bp_login.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = 'select * from lusers where username = "%s"' % (data['username'])
        res = dbs.findone(sql)
        if res:
            if res['password'] == data['password']:
                return jsonify({'code': 200, 'userid': res['id'], 'username': res['username']})
            else:
                return jsonify({'code': 20011, 'mes': '密码错误'})
        else:
            return jsonify({'code': 20012, 'mes': '用户不存在'})


@bp_login.route('/sfM', methods=['POST', 'GET'])
def sfM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        token = data['token']
        uid = data['uid']
        sql = """select * from sf_login where token = '%s' and uid = '%s'""" % (token, uid)
        res = dbs.findone(sql)
        if res:

            return jsonify({'code': 200, 'url': 'http://localhost:8080/#/', 'userid': res['userid']})
        else:
            return jsonify({"url": 'http://localhost:8080/#/sanfang'})
            # return jsonify('http://localhost:8080/#/sanfang')


@bp_login.route('/dengluM', methods=['POST', 'GET'])
def dengluM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        token = data['token']
        mobile = data['username']
        uid = int(data['uid'])
        sql9 = 'select id from lusers where username = "%s"' % (mobile)
        res9 = dbs.findone(sql9)
        if not res9:
            sql = """insert into lusers(username) values ('%s')""" % (mobile)
            dbs.update(sql)
        sql1 = """select * from lusers where username= ('%s')""" % (mobile)
        res = dbs.findone(sql1)
        sql2 = """insert into sf_login(userid,uid,token) values (%d,%d,'%s')""" % (int(res['id']), uid, token)
        dbs.update(sql2)
        return jsonify({'code': 200, "mes": "登录成功", "url": 'http://localhost:8080', 'userid': res['id']})


@bp_login.route('/uidtoken')
def uidtoken():
    uid = '123456789'
    token = 'qwert789'
    return jsonify({'code': 200, 'uid': uid, 'ltoken': token})


import datetime
import random


@bp_login.route('/shop', methods=['POST', 'GET'])
def shop():
    if request.method == 'GET':
        sql = 'select * from shop'
        res = dbs.findall(sql)
        return jsonify({'code': 200, 'list': res})
    elif request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        order_no = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S") + str(data['userid']) + str(
            random.randint(1000, 9999))
        sql1 = 'select price from shop where id = %d' % (int(data['id']))
        res1 = dbs.findone(sql1)
        print(res1['price'])
        sql2 = '''insert into order1(userid,shopid,order_no,status,money) values(%d,%d,"%s",%d,%.1f)''' % (
            int(data['userid']), int(data['id']), str(order_no), 1, float(res1['price']))
        dbs.update(sql2)
        print(sql2)
        return jsonify({'code': 200, 'order_no': order_no})


@bp_login.route('/order', methods=['POST', 'GET'])
def order():
    if request.method == 'GET':
        order_no = request.args.get('order_no')
        sql = 'select * from order1 where order_no = "%s"' % (order_no)
        res = dbs.findone(sql)
        return jsonify({'code': 200, 'list': res})

    elif request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        liushui = '123456789'
        sql = 'update order1 set status = 2,liushui = "%s" where order_no = "%s"' % (liushui, data['order_no'])
        dbs.update(sql)
        sql2 = 'select shopid from order1 where order_no = "%s"' % (data['order_no'])
        res2 = dbs.findone(sql2)
        sql3 = 'select xiaol from shop where id = %d' % (int(res2['shopid']))
        res3 = dbs.findone(sql3)
        count = int(res3['xiaol'])
        count += 1
        sql4 = 'update shop set xiaol = %d where id = %d' % (int(count), int(res2['shopid']))
        dbs.update(sql4)
        return jsonify({'code': 200})


@bp_login.route('/dsuser', methods=['POST', 'GET'])
def dsuser():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = 'select id from dsuser where name = "%s" and mobile = "%s"' % (data['name'], data['mobile'])
        res = dbs.findone(sql)
        if res:
            return jsonify({'code': 200, 'userid': res['id']})
        else:
            return jsonify({'code': 20010})
    elif request.method == 'GET':
        sql = 'select * from order1'
        res = dbs.findall(sql)
        return jsonify({'code': 200, 'list': res})


@bp_login.route('/fahuo', methods=['POST'])
def fahuo():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form

        sql1 = 'select name,mobile,address from dsuser where id = %d' % (int(data['userid']))
        res1 = dbs.findone(sql1)
        print(res1)
        sql2 = 'select address,gongsi from shop where id = %d' % (int(data['id']))
        res2 = dbs.findone(sql2)
        print(res2)
        url = 'http://127.0.0.1:8000/wuliu/wuliu?name='+res1['name']+'&mobile='+res1['mobile']+'&maddress='+res1['address']+'&faddress='+res2['address']+'&gongsi='+res2['gongsi']
        return jsonify({'code':200,'url':url})